package com.dqp.cslggroup.MenuViewPage;

import android.Manifest;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.os.StrictMode;
import android.provider.MediaStore;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.bumptech.glide.Glide;
import com.dqp.cslggroup.R;
import com.dqp.cslggroup.UI.MyApplication;
import com.dqp.cslggroup.Util.ToastUtil;
import com.dqp.cslggroup.Util.UiUtils;
import com.dqp.cslggroup.bean.note;
import com.dqp.cslggroup.greendao.noteDao;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Objects;

import pub.devrel.easypermissions.AppSettingsDialog;
import pub.devrel.easypermissions.EasyPermissions;

import static android.app.Activity.RESULT_OK;

public class menu_Fragment1 extends Fragment {
    private View view;
    private String takePhotoPath;
    private int REQUEST_CODE_CAMERA = 1;
    private ImageView img;
    private Context context;
    private static String[] permissions = {
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.READ_CALENDAR,
            Manifest.permission.WRITE_CALENDAR,
            Manifest.permission.READ_EXTERNAL_STORAGE
    };
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.my_note, null);
        context = view.getContext();
        Objects.requireNonNull(getActivity()).getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
        TextView name=view.findViewById(R.id.note_course_name);
        TextView data=view.findViewById(R.id.note_data);
        EditText noteEd=view.findViewById(R.id.mynote);
        Button save=view.findViewById(R.id.btn_save);
        Button cancel=view.findViewById(R.id.btn_cancel);
        img=view.findViewById(R.id.img);
        TextView take=view.findViewById(R.id.take_picture);
        @SuppressLint("SimpleDateFormat") SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        Date d = new Date(System.currentTimeMillis());
        data.setText(simpleDateFormat.format(d));
        cancel.setOnClickListener(v -> {
            img.setVisibility(View.GONE);
            noteEd.setText("");
            @SuppressLint("SimpleDateFormat") SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            Date date1 = new Date(System.currentTimeMillis());
            data.setText(simpleDateFormat1.format(date1));
        });
        take.setOnClickListener(v -> {
            if(EasyPermissions.hasPermissions(context,permissions)){
                StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
                StrictMode.setVmPolicy(builder.build());
                builder.detectFileUriExposure();
                takePhotoPath = Environment.getExternalStorageDirectory().getPath() + "/CSLG_Note" + Calendar.getInstance().getTimeInMillis() + ".jpg";
                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                Uri photoUri = Uri.fromFile(new File(takePhotoPath)); // 传递路径
                intent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);// 更改系统默认存储路径
                startActivityForResult(intent, REQUEST_CODE_CAMERA);
            } else  new AppSettingsDialog.Builder(this)
                    .setTitle("权限申请")
                    .setRationale("您没有授权应用所需的读写日历的相关权限，此功能无法运行。请打开应用设置以修改应用权限。")
                    .build().show();
        });
        save.setOnClickListener(v12 -> {
            UiUtils.hideKeyboard(view);
            String note=noteEd.getText().toString();
            if ( note.length()>0 || takePhotoPath!=null) {
                note newNote = new note();
                newNote.setData(data.getText().toString());
                newNote.setName(name.getText().toString());
                newNote.setText(noteEd.getText().toString());
                newNote.setImg(takePhotoPath);
                MyApplication.getDaoInstant().getNoteDao().insertOrReplace(newNote);
                img.setImageResource(0);
                img.setVisibility(View.GONE);
                noteEd.setText("");
                @SuppressLint("SimpleDateFormat") SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yyyy-MM-dd HH:mm");
                Date date1 = new Date(System.currentTimeMillis());
                data.setText(simpleDateFormat1.format(date1));
                ToastUtil.showToast("添加完成");
            }else ToastUtil.showToast("无内容");
        });
        return view;
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CODE_CAMERA && resultCode == RESULT_OK) {
            Glide.with(context).load(takePhotoPath).into(img);
            img.setVisibility(View.VISIBLE);
        }
    }

}
